package com.java.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author jiangli
 * @date 2019/10/27 20:48
 */
public class NettyDemo {

	public static void main(String[] args) throws Exception {
		//定义一对线程组
		//主线程组,用于接收客户端的连接,不做任何处理,跟老板一样不做事
		EventLoopGroup materGroup = new NioEventLoopGroup();
		//从线程组,主线程组把任务丢给他,让手下线程组去做任务
		EventLoopGroup workerGroup = new NioEventLoopGroup();

		try {
			//netty服务器的创建,serverBootstrap是一个启动类
			ServerBootstrap serverBootstrap = new ServerBootstrap();
			serverBootstrap.group(materGroup,workerGroup)  //设置主从线程组
					.channel(NioServerSocketChannel.class) //设置nio的双向通道
					.childHandler(new ServerInitializer());                   //子处理器,用于处理workerGroup

			//启动server,设置端口为8088,同步方式启动
			ChannelFuture channelFuture = serverBootstrap.bind(8088).sync();
			//监听关闭的channel,设置为同步方式
			channelFuture.channel().closeFuture().sync();
		} finally {
			//关闭线程组
			materGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
	}
}
